package com.raja.servingStation.common.util;


public class ByteUtil<T extends Object> {
	
	public static long byte4ToLong(byte[] paramArrayOfByte, int paramInt) {
		long l = 0L;
		if (null != paramArrayOfByte)
			for (int i = 3; i >= 0; i--) {
				l <<= 8;
				l |= paramArrayOfByte[(paramInt + i)] & 0xFF;
			}
		return l;
	}
	public static byte[] intToByteArray(int value) {
		byte[] b = new byte[4];
		for (int i = 0; i < 4; i++) {
			int offset = (b.length - 1 - i) * 8;
			b[i] = (byte) ((value >>> offset) & 0xFF);
		}
		return b;
	}
	
	public static byte[] int8ToByteArray(int value) {
		byte[] b = new byte[8];
		for (int i = 0; i < 8; i++) {
			int offset = (b.length - 1 - i) * 8;
			b[i] = (byte) ((value >>> offset) & 0xFF);
		}
		return b;
	}

	public static byte[] longToByte(long n) {
		byte[] buf = new byte[4];
		buf[0] = (byte) (n & 0x000000FF);
		buf[1] = (byte) ((n & 0x0000FF00) >> 8);
		buf[2] = (byte) ((n & 0x00FF0000) >> 16);
		buf[3] = (byte) ((n & 0xFF000000) >> 24);
		return buf;
	}

	public static short[] byteToShort(byte[] val) {
		short[] buf = new short[val.length];
		for (int i = 0; i < val.length; i++) {
			buf[i] = (short) val[i];
			if (buf[i] < 0) {
				buf[i] = (short) (buf[i] + 256);
			}
		}
		return buf;
	}

	public static byte[] shortToByte(short[] val) {
		byte[] buf = new byte[val.length];
		for (int i = 0; i < val.length; i++) {
			buf[i] = (byte) val[i];
		}
		return buf;
	}

	public static byte[] int1ToByteArray(int value) {
		byte[] b = new byte[1];
		for (int i = 0; i < 1; i++) {
			int offset = (b.length - 1 - i) * 8;
			b[i] = (byte) ((value >>> offset) & 0xFF);
		}
		return b;
	}

	public static byte[] int2ToByteArray(int value) {
		byte[] b = new byte[2];
		for (int i = 0; i < 2; i++) {
			int offset = (b.length - 1 - i) * 8;
			b[i] = (byte) ((value >>> offset) & 0xFF);
		}
		return b;
	}

	public static byte[] longToByteArray(Long value) {
		byte[] b = new byte[4];
		for (int i = 0; i < 4; i++) {
			int offset = (b.length - 1 - i) * 8;
			b[i] = (byte) ((value >>> offset) & 0xFF);
		}
		return b;
	}

	public static byte[] long8ToByteArray(Long value) {
		byte[] b = new byte[8];
		for (int i = 0; i < 8; i++) {
			int offset = (b.length - 1 - i) * 8;
			b[i] = (byte) ((value >>> offset) & 0xFF);
		}
		return b;
	}
	
	//java 合并两个byte数组
	public static byte[] byteMerger(byte[] byte_1, byte[] byte_2){
		byte[] byte_3 = new byte[byte_1.length+byte_2.length];
		System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
		System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
		return byte_3;
	}
	//	little-endian
	public static int byte1ToLong(byte bt) {
		int n = 0;
		n = bt;
		if (bt < 0)
			n += 256;
		return n;
	}
	//	little-endian
	public static int byte2ToLong(byte[] buf, int start) {
		int n = 0;
		if (null != buf) {
			for (int i = 1; i >= 0; i--) {
				n <<= 8;
				n |= (buf[start + i] & 0x00ff);
			}
		}
		return n;
	}
}
